<?php
/**
 * ZView
 *
 * Zend Frameworks View Component for TYPO3
 *
 * @package    ZView
 * @copyright  Copyright (c) Zephir AG, www.zephir.ch
 */

/** Abstract class for extension */
require_once 'Zend/View/Helper/Abstract.php';

/**
 * Helper to generate a "typolink" element
 *
 * @package ZView
 * @copyright  Copyright (c) Zephir AG, www.zephir.ch
 */
class Tx_Zview_Helper_T3typolink extends Zend_View_Helper_Abstract
{

	/**
	 * The TYPO3 plugin
	 *
	 * @var tslib_pibase
	 */
	private $_pi;

	/**
	 * Generates a link to the current page (the caller page) using PI vars.
	 * This is the shortform of link()->thisPagePIvars().
	 *
	 * If no arguments are passed, returns a Tx_Zview_Helper_T3link object.
	 *
	 * @param array $param
	 * @param string $label
	 * @return string|Tx_Zview_Helper_T3link
	 */
	public function t3typolink($label='', $param='', $urlParameters=array(), $target='')
	{
        if (0 == func_num_args()) {
            return $this;
        }
		return $this->link($label, $param, $urlParameters, $target);
	}

	/**
	 * Returns a linked string made from typoLink parameters (this is a wrapper for tslib_cObj::getTypoLink).
	 *
	 * This function takes $label as a string, wraps it in a link-tag based on the $params string, which should contain data like that you would normally pass to the popular <LINK>-tag in the TSFE.
	 * Optionally you can supply $urlParameters which is an array with key/value pairs that are rawurlencoded and appended to the resulting url.
	 *
	 * @param	string		Text string being wrapped by the link.
	 * @param	string		Link parameter; eg. "123" for page id, "kasperYYYY@typo3.com" for email address, "http://...." for URL, "fileadmin/blabla.txt" for file.
	 * @param	array		An array with key/value pairs representing URL parameters to set. Values NOT URL-encoded yet.
	 * @param	string		Specific target set, if any. (Default is using the current)
	 * @return	string		The wrapped $label-text string
	 * @see getTypoLink_URL()
	 */
	public function link($label, $param, $urlParameters=array(), $target='')
	{
		return $this->view->getPi()->cObj->getTypoLink($label, $param, $urlParameters, $target);
	}

	/**
	 * Returns the URL of a "typolink" create from the input parameter string, url-parameters and target (this is a wrapper for tslib_cObj::getTypoLink_URL).
	 *
	 * @param string $param Link parameter; eg. "123" for page id, "kasperYYYY@typo3.com" for email address, "http://...." for URL, "fileadmin/blabla.txt" for file.
	 * @param array $urlParameters An array with key/value pairs representing URL parameters to set. Values NOT URL-encoded yet.
	 * @return string The URL
	 */
	public function url($param, $urlParameters=array())
	{
		return $this->view->getPi()->cObj->getTypoLink_URL($param, $urlParameters);
	}

    /**
     * Set the View object
     *
     * @param  Zend_View_Interface $view
     * @return Zend_View_Helper_Abstract
     */
	public function setView(Zend_View_Interface $view)
	{
		$this->view = $view;
		$this->_pi = $view->getPi();
		if(empty($this->_pi) || (!$this->_pi instanceof tslib_pibase)) {
			throw new Exception('Please set tslib_pibase using $view->setPi($this) in your plugin class.');
		}
        return $this;
	}

}

if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zview/helper/T3typolink.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zview/helper/T3typolink.php']);
}
